<?php

namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class OperationLogs extends BaseModel
{

    protected $connection = 'mysql';
    protected $table = 'operation_logs';
    private $fields = ['controller', 'method', 'data_id', 'request', 'admin_name', 'created_at'];

    const ACCOUNT = 'account';
    const BANNER = 'banner';
    const PERMISSION = 'permission';
    const GIFT = 'gift';
    const GOODS = 'goods';
    const NUMBER = 'number';
    const ROLE = 'role';
    const ROOM = 'room';
    const SYSTEM = 'system';
    const USER = 'user';
    const VERSION = 'version';
    const FEEDBACK = 'feedback';
    const CUSTOM_MSG = 'custom_msg';
    const FRIENDS_REPORT = 'friends_report';
    const ROOM_REPORT = 'room_report';
    const DIAMOND_RECORD = 'diamond_record';
    const SEND_GOODS_RECORD = 'send_goods_record';
    const TRENDS = 'trends';
    const GENERATION_CHARGE_RECORD = 'generation_charge_record';
    const FINANCE = 'finance';
    const UNION = 'union';
    const ORIGAMI = 'origamiTopics';
    const UNION_PAY_ORDER = 'unionPayOrder';
    const MALL_MAIN = 'MallMain';
    const ACTIVITY = 'activity';

    const CONTROLLER_MAP = [
        self::ACCOUNT => '账目管理',
        self::BANNER => '广告管理',
        self::PERMISSION => '权限管理',
        self::GIFT => '礼物管理',
        self::GOODS => '商品',
        self::NUMBER => '用户管理',
        self::ROLE => '角色管理',
        self::ROOM => '房间管理',
        self::SYSTEM => '系统管理',
        self::USER => '管理员',
        self::VERSION => '版本管理',
        self::FEEDBACK => '用户反馈',
        self::CUSTOM_MSG => '自定义消息',
        self::FRIENDS_REPORT => '用户举报',
        self::ROOM_REPORT => '房间举报',
        self::DIAMOND_RECORD => '扣款/赠送记录',
        self::SEND_GOODS_RECORD => '赠送礼物',
        self::TRENDS => '动态管理',
        self::GENERATION_CHARGE_RECORD => '代充',
        self::FINANCE => '财务管理',
        self::UNION => '公会管理',
        self::ORIGAMI => '折纸管理',
        self::UNION_PAY_ORDER => '银联支付订单管理',
        self::MALL_MAIN => '商城CMS管理',
        self::ACTIVITY => '活动',
    ];

    public static function createData($request, $controller, $method, $dataId = '')
    {
        $user = Users::getCurrentUser();
        $model = new OperationLogs();
        $model->controller = $controller;
        $model->method = $method;
        $model->data_id = is_array($dataId) ? implode(',', $dataId) : $dataId;
        $model->request = json_encode($request->all(), JSON_UNESCAPED_UNICODE);
        $model->admin_id = $user->id ?? 0;
        $model->admin_name = $user->name ?? "";
        $model->ip = $request->ip() ?? "";
        return $model->save();
    }

    public function getList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $controller = $request->input('controller', '');
        $method = $request->input('method', '');
        $desc = $request->input('desc', '');
        $adminId = $request->input('admin_id', '');
        $startTime = $request->input('start_time', '');
        $endTime = $request->input('end_time', '');

        $query = DB::connection($this->connection)->table($this->table);
        if (!empty($controller))
            $query->where('controller', '=', $controller);
        if (!empty($method))
            $query->where('method', '=', $method);
        if (!empty($adminId))
            $query->where('admin_id', '=', $adminId);
        if (!empty($desc))
            $query->where('request', 'like', "%{$desc}%");
        if (!empty($startTime))
            $query->where('created_at', '>=', $startTime);
        if (!empty($endTime))
            $query->where('created_at', '<=', $endTime);

        $lists = $query->orderBy('id', 'DESC')->paginate($limit, $this->fields, 'page', $page);
        $lists = $this->objToArray($lists);

        if(!empty($lists['data'])){
            foreach ($lists['data'] as &$r){
                $r['controller'] = self::CONTROLLER_MAP[$r['controller']] ?? $r['controller'];
            }
        }

        return $lists;
    }

    public function getCates()
    {
        return self::CONTROLLER_MAP;
    }
}
